package com.template.controller;


import com.template.entity.Department;
import com.template.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yumuyang
 * @since 2021-11-01
 */

@Controller
public class DepartmentController {

    @Autowired
    DepartmentService departmentService;

    @PostMapping("/addDepartment")
    public String addDepartment(Department department) {
        departmentService.saveDepartment(department);

        return "redirect:/departmentList";
    }

    @GetMapping("/deleteDepartment")
    public String deleteDepartment(@RequestParam("id") Integer id,
                                   @RequestParam(value = "pn", defaultValue = "1") Integer pn,
                                   RedirectAttributes redirectAttributes) {
        departmentService.deleteDepartmentById(id);

        redirectAttributes.addAttribute("pn", pn);

        return "redirect:/departmentList";
    }

    @PostMapping("/updateDepartment")
    public String updateDepartment(@RequestParam(value = "pn", defaultValue = "1") Integer pn,
                                   Department department,
                                   RedirectAttributes redirectAttributes) {
        departmentService.updateDepartment(department);

        redirectAttributes.addAttribute("pn", pn);

        return "redirect:/departmentList";
    }

}

